/**
 * Created by SZ on 2018/05/23.
 */
const express = require('express');
const User = require("../models/user");
const router = express.Router();
/* GET users listing. */
router.get('/', (req, res) => {
    User.find({super: {$ne: 1}}).lean()
        .select('name status')
        .exec((err, docs) => {
            if (err) {
                res.status(404);
                res.json(err);
            } else {
                res.status(200);
                res.json(docs);
            }
        });
});

/* find user by id. */
router.get('/detail/:id', (req, res) => {
    User.findOne({_id: req.params.id}).lean()
        .select('-password')
        .exec((err, doc) => {
            if (err) {
                res.status(500);
                res.json(err);
            } else {
                res.status(200);
                res.json(doc);
            }
        });
});

/* create user. */
router.post('/create', (req, res) => {
    User.findOne({name: req.body.user.name}, (err, user) => {
        if (err) {
            res.status(500);
            res.json(err);
        } else {
            if (user) {
                res.status(200);
                res.json({
                    result: false,
                    code: 105,
                    msg: "用户已存在！"
                });
            } else {
                const user = new User(req.body.user);
                user.register();
                user.save((err, doc) => {
                    if (err) {
                        res.status(500);
                        res.json(err);
                    } else {
                        res.status(200);
                        res.json({
                            result: true,
                            doc: doc
                        });
                    }
                });
            }
        }
    });
});

/* update user by id. */
router.post('/:id/update', (req, res) => {
    User.findOne({
        $and: [
            {_id: {$ne: req.params.id}},
            {name: req.body.user.name}
        ]
    }).exec((err, doc) => {
        if (err) {
            res.status(500);
            res.json(err);
        } else {
            if (doc) {
                res.status(200);
                res.json({
                    result: false,
                    code: 105,
                    msg: "用户已存在！"
                });
            } else {
                User.findOneAndUpdate({_id: req.params.id}, req.body.user, {new: true}, (err, doc) => {
                    if (err) {
                        res.status(500);
                        res.json(err);
                    } else {
                        res.status(200);
                        res.json({result: true});
                    }
                });
            }
        }
    });
});

/* remove user by id. */
router.post('/:id/remove', (req, res) => {
    User.findOneAndRemove({_id: req.params.id}, (err, doc) => {
        if (err) {
            res.status(500);
            res.json(err);
        } else {
            res.status(200);
            res.json({result: true});
        }
    });
});


module.exports = router;

